/* ========================================================================
 * Bootstrap: navbar.js v3.3.4
 * http://getbootstrap.com/javascript/#navbars
 * ========================================================================
 * Copyright 2011-2015 Qiansuit, Inc.
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * ======================================================================== */


+function ($) {
    'use strict';

    // Navbar CLASS DEFINITION
    // ====================

    var Navbar = function (element) {
        this.element = $(element);
    };

    Navbar.VERSION = '3.3.4';

    Navbar.TRANSITION_DURATION = 150;

    Navbar.prototype.show = function () {
        var $this = this.element;
        var $ul = $this.closest('ul:not(.dropdown-menu)');
        if ($this.parent('li').hasClass('active')) {
            return;
        }

        this.activate($this.closest('li'), $ul);
    };

    Navbar.prototype.activate = function (element, container, callback) {
        var $active = container.find('> .active');
        var transition = callback && $.support.transition && (($active.length && $active.hasClass('fade')) || !!container.find('> .fade').length);

        function next() {
            $active
                    .removeClass('active')
                    .find('> .dropdown-menu > .active')
                    .removeClass('active')
                    .end()
                    .find('[data-toggle="navbar"]')
                    .attr('aria-expanded', false);

            element
                    .addClass('active')
                    .find('[data-toggle="navbar"]')
                    .attr('aria-expanded', true);

            if (transition) {
                element[0].offsetWidth; // reflow for transition
                element.addClass('in');
            } else {
                element.removeClass('fade');
            }

            if (element.parent('.dropdown-menu').length) {
                element
                        .closest('li.dropdown')
                        .addClass('active')
                        .end()
                        .find('[data-toggle="navbar"]')
                        .attr('aria-expanded', true);
            }

            callback && callback();
        }

        $active.length && transition ?
                $active.one('bsTransitionEnd', next).emulateTransitionEnd(Navbar.TRANSITION_DURATION) :
                next();
    };


    // Navbar PLUGIN DEFINITION
    // =====================

    function Plugin(option) {
        return this.each(function () {
            var $this = $(this);
            var data = $this.data('bs.navbar');
            if (!data) {
                $this.data('bs.navbar', (data = new Navbar(this)));
            }
            if (typeof option === 'string') {
                data[option]();
            }
        });
    }

    var old = $.fn.navbar;

    $.fn.navbar = Plugin;
    $.fn.navbar.Constructor = Navbar;


    // Navbar NO CONFLICT
    // ===============

    $.fn.navbar.noConflict = function () {
        $.fn.navbar = old;
        return this;
    };


    // Navbar DATA-API
    // ============

    var clickHandler = function (e) {
        //e.preventDefault();
        Plugin.call($(this), 'show');
    };

    $(document).on('click.bs.navbar.data-api', '[data-toggle="navbar"]', clickHandler);
            //.on('click.bs.navbar.data-api', '[data-toggle="pill"]', clickHandler);
}(jQuery);
